package com.weiwei.diancan.view;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.weiwei.diancan.R;

/**
 * Created by BMW on 2016/11/6.
 */
public class LoginDialog extends Dialog {

    private final TextView tv_update;
    private final EditText et_pass;
    private final EditText et_num;
    private Activity context;

    private String num;
    private String pass;

    private OnUpdateListener listener;

    public void setListener(OnUpdateListener listener) {
        this.listener = listener;
    }

    public EditText getEt_pass() {
        return et_pass;
    }

    public EditText getEt_num() {
        return et_num;
    }

    public LoginDialog(Activity context) {
        super(context, R.style.share_dialog);
        this.context = context;
        View v = LayoutInflater.from(context).inflate(R.layout.login_dialog, null);
        et_num = (EditText)v.findViewById(R.id.et_num);
        et_pass = (EditText)v.findViewById(R.id.et_pass);
        tv_update = (TextView)v.findViewById(R.id.tv_update);

        et_pass.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                pass = s.toString();
                if(TextUtils.isEmpty(num)||TextUtils.isEmpty(pass)){
                    tv_update.setTextColor(Color.parseColor("#777777"));
                }else{
                    tv_update.setTextColor(Color.parseColor("#181818"));
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        et_num.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                num = s.toString();
                if(TextUtils.isEmpty(num)||TextUtils.isEmpty(pass)){
                    tv_update.setTextColor(Color.parseColor("#777777"));
                }else{
                    tv_update.setTextColor(Color.parseColor("#181818"));
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        tv_update.setOnClickListener(v1 -> {
            listener.onClick(v1,et_num.getText().toString(),et_pass.getText().toString());
        });
        addContentView(v,
                new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT));
    }

    public interface OnUpdateListener{
        void onClick(View v, String num,String pass);
    }

}
